//引入2个方法createRouter,createWebHashHistory
import { createRouter, createWebHashHistory } from 'vue-router'
import Index from '~/pages/index.vue'
import About from '~/pages/about.vue'
import NotFound from '~/pages/404.vue'
import Login from '~/pages/login.vue'
import Admin from '~/layouts/admin.vue'
import GoodsList from '~/pages/goods/list.vue'
import CategoryList from '~/pages/category/list.vue'
import UserList from '~/pages/user/list.vue'
import OrderList from '~/pages/order/list.vue'
import CommentList from '~/pages/comment/list.vue'
import SettingBase from '~/pages/setting/base.vue'
import ImageList from '~/pages/image/list.vue'
import NoticeList from '~/pages/notice/list.vue'
import CouponList from '~/pages/coupon/list.vue'
import ManagerList from '~/pages/manager/list.vue'


// 定义一个空数组，用于存放路由
// const routes = [{
//         path: '/',
//         component: Admin,
//         // 子路由
//         children: [{
//             path: '/',
//             component: Index,
//             meta: {
//                 title: '首页'
//             }
//         },{
//             path: '/goods/list',
//             component: GoodsList,
//             meta: {
//                 title: '商品管理'
//             }
//         },{
//             path: '/category/list',
//             component: CategoryList,
//             meta: {
//                 title: '分类列表'
//             }
//         }]

//     }, {
//         path: '/about',
//         component: About,
//         meta: {
//             title: '关于我们'
//         }
//     }, {
//         path: '/login',
//         component: Login,
//         meta: {
//             title: '登录'
//         }
//     },
//     {
//         path: '/:pathMatch(.*)*',
//         component: NotFound
//     }
// ]

//默认路由，所有用户共享
const routes = [
    {
            path: '/',
            name: 'admin',
            component: Admin,
    },         
    {       path: '/login',
            component: Login,
            meta: {
                title: '登录'
            }
    },
        {
            path: '/:pathMatch(.*)*',
            component: NotFound
        }
    ]

    // 动态路由，用于匹配菜单动态添加路由
    const asyncRouters = [
                   {
                        path: '/',
                        name: '/',
                        component: Index,
                        meta: {
                            title: '首页'
                        }
                    },{
                        path: '/goods/list',
                        name: '/goods/list',
                        component: GoodsList,
                        meta: {
                            title: '商品管理'
                        }
                    },{
                        path: '/category/list',
                        name: '/category/list',
                        component: CategoryList,
                        meta: {
                            title: '分类列表'
                        }
                    },{
                        path: '/user/list',
                        name: '/user/list',
                        component: UserList,
                        meta: {
                            title: '用户列表'
                        }
                    },{
                        path: '/order/list',
                        name: '/order/list',
                        component: OrderList,
                        meta: {
                            title: '订单列表'
                        }
                    },{
                        path: '/comment/list',
                        name: '/comment/list',
                        component: CommentList,
                        meta: {
                            title: '评价列表'
                        }
                    },{
                        path: '/image/list',
                        name: '/image/list',
                        component: ImageList,
                        meta: {
                            title: '图库列表'
                        }
                    },{
                        path: '/notice/list',
                        name: '/notice/list',
                        component: NoticeList,
                        meta: {
                            title: '公告列表'
                        }
                    },{
                        path: '/setting/base',
                        name: '/setting/base',
                        component: SettingBase,
                        meta: {
                            title: '配置列表'
                        }
                    },{
                        path: '/coupon/list',
                        name: '/coupon/list',
                        component: CouponList,
                        meta: {
                            title: '优惠券列表'
                        }
                    },{
                        path:"/manager/list",
                        name:"/manager/list",
                        component:ManagerList,
                        meta:{
                            title:"管理员管理"
                        }
                    }
    ]

// 创建一个路由，使用createRouter函数，传入一个对象，包含history和routes属性
export const router = createRouter({
    // 使用createWebHashHistory函数创建一个history对象
    history: createWebHashHistory(),
    // 传入routes属性
    routes
})

// 导出默认的路由
// export default router

// 动态添加路由的方法
export function addRoutes(menus) {
    // 是否有新的路由
    let hasNewRoutes = false
    const findAddRoutesByMenus=(arr)=>{
        arr.forEach(e => {
            let item=asyncRouters.find(o=>o.path==e.frontpath)
            if(item && !router.hasRoute(item.path)){
                router.addRoute("admin",item)
                hasNewRoutes=true
            }
            if(e.child && e.child.length>0){
                findAddRoutesByMenus(e.child)
            }
        })
    }
    findAddRoutesByMenus(menus)
    // console.log(router.getRoutes())
    return hasNewRoutes

}